package com.tencent.sr.iris.activity.service.activity.impl;

import com.tencent.sr.iris.activity.common.util.TimeUtils;
import com.tencent.sr.iris.activity.interfaces.enums.ActivityRecordStatusEnum;
import com.tencent.sr.iris.activity.repository.singlemapper.example.TIrisUserActivityRecordExample;
import com.tencent.sr.iris.activity.service.activity.ActivityRecordQueryHandler;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.stereotype.Component;

import java.util.Date;

import static cn.hutool.core.util.StrUtil.toCamelCase;

/**
 * Copyright (C) 2023 Limited, a Tencent company. All rights reserved.
 *
 * @author p_htaozhou@tencent.com
 * @create 2023/8/16 19:29
 */
public abstract class AbstractActivityRecordQueryHandler {


    /**
     * 邀请有礼查询处理器实现
     */
    @Component("INVITE_GIFT_QUERY_HANDLER")
    static class InviteGiftActivityRecordQueryHandler implements ActivityRecordQueryHandler {

        /**
         * 已完成、未开始、未完成
         *
         * @param query  查询条件
         * @param status 状态
         */
        @Override
        public void statusFilterWrap(BoolQueryBuilder query, Integer status) {
            switch (status) {
                // 已完成
                case 1:
                    // 已完成口径：预估奖励金 > 0
                    query.must(QueryBuilders.rangeQuery(toCamelCase(TIrisUserActivityRecordExample.ESTIMATE_AWARD_AMOUNT))
                            .gt(0));
                    break;
                // 未完成
                case 2:
                    // 未完成口径：任务结束时间过了 且 活动状态为未完成(已完成可能存在用户退货场景，GMV为0)
                    query.must(QueryBuilders.rangeQuery(toCamelCase(
                                    TIrisUserActivityRecordExample.CURRENT_ACTIVITY_END_TIME)).lt(System.currentTimeMillis()))
                            .must(QueryBuilders.termQuery(toCamelCase(TIrisUserActivityRecordExample.ACTIVITY_STATUS),
                                    ActivityRecordStatusEnum.UNFINISHED.getCode()));
                    break;
                // 未开始
                case 4:
                    // 未开始口径： 任务开始时间在30天内(未结束)：预估奖励金 = 0
                    query.must(QueryBuilders.rangeQuery(toCamelCase(
                                    TIrisUserActivityRecordExample.CURRENT_ACTIVITY_END_TIME)).gte(TimeUtils.getAfterDaysDate(1).getTime()))
                            .must(QueryBuilders.termQuery(toCamelCase(TIrisUserActivityRecordExample.ESTIMATE_AWARD_AMOUNT), 0));
                    break;
                default:
                    break;
            }
        }
    }

    /**
     * 月月领查询处理器
     */
    @Component("MONTH_GIFT_QUERY_HANDLER")
    static class MonthGiftActivityRecordQueryHandler implements ActivityRecordQueryHandler {

        /**
         * 进行中、未开始
         *
         * @param query  查询条件
         * @param status 状态
         */
        @Override
        public void statusFilterWrap(BoolQueryBuilder query, Integer status) {
            BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
            switch (status) {
                // 进行中
                case 0:
                    // 进行中口径：分享数 > 0 或者 佣金佣金 > 0 或者 预估佣金大于0
                    BoolQueryBuilder commissionQuery = QueryBuilders.boolQuery();
                    commissionQuery.must(QueryBuilders.rangeQuery(toCamelCase(TIrisUserActivityRecordExample.COMMISSION)).gt(0));

                    BoolQueryBuilder estimateCommissionQuery = QueryBuilders.boolQuery();
                    estimateCommissionQuery.must(QueryBuilders.rangeQuery(toCamelCase(TIrisUserActivityRecordExample.ESTIMATE_COMMISSION)).gt(0));

                    BoolQueryBuilder shareCountQuery = QueryBuilders.boolQuery();
                    shareCountQuery.must(QueryBuilders.rangeQuery(toCamelCase(TIrisUserActivityRecordExample.SHARE_COUNT)).gt(0));

                    queryBuilder.should(commissionQuery).should(estimateCommissionQuery).should(shareCountQuery);
                    query.must(queryBuilder);
                    break;
                // 未开始
                case 4:
                    // 进行中口径：分享数 = 0 并且 佣金佣金 = 0 并且 预估佣金 = 0
                    query.must(QueryBuilders.termQuery(toCamelCase(TIrisUserActivityRecordExample.COMMISSION), 0))
                            .must(QueryBuilders.termQuery(toCamelCase(TIrisUserActivityRecordExample.ESTIMATE_COMMISSION), 0))
                            .must(QueryBuilders.termQuery(toCamelCase(TIrisUserActivityRecordExample.SHARE_COUNT), 0));
                    break;
                default:
                    break;
            }
        }
    }

    /**
     * 特邀有礼查询处理器
     */
    @Component("SPECIAL_INVITE_REWARD_QUERY_HANDLER")
    static class SpecialInviteRewardActivityRecordQueryHandler implements ActivityRecordQueryHandler {

        /**
         * 未开始、进行中、已完成
         *
         * @param query  查询条件
         * @param status 状态
         */
        @Override
        public void statusFilterWrap(BoolQueryBuilder query, Integer status) {
            BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
            switch (status) {
                // 进行中
                case 0:
                    // 进行中口径：分享数 > 0 或者 佣金佣金 > 0 或者 预估佣金大于0
                    BoolQueryBuilder commissionQuery = QueryBuilders.boolQuery();
                    commissionQuery.must(QueryBuilders.rangeQuery(toCamelCase(TIrisUserActivityRecordExample.COMMISSION)).gt(0));

                    BoolQueryBuilder estimateCommissionQuery = QueryBuilders.boolQuery();
                    estimateCommissionQuery.must(QueryBuilders.rangeQuery(toCamelCase(TIrisUserActivityRecordExample.ESTIMATE_COMMISSION)).gt(0));

                    BoolQueryBuilder shareCountQuery = QueryBuilders.boolQuery();
                    shareCountQuery.must(QueryBuilders.rangeQuery(toCamelCase(TIrisUserActivityRecordExample.SHARE_COUNT)).gt(0));

                    queryBuilder.should(commissionQuery).should(estimateCommissionQuery).should(shareCountQuery);

                    Date beforeDate = TimeUtils.timeSub(TimeUtils.getCurrentDayFirstSecondTime(), 30);
                    query.must(QueryBuilders.rangeQuery(toCamelCase(
                                    TIrisUserActivityRecordExample.CURRENT_ACTIVITY_START_TIME)).gt(beforeDate.getTime()))
                            .mustNot(QueryBuilders.termQuery(toCamelCase(TIrisUserActivityRecordExample.ACTIVITY_STATUS),
                                    ActivityRecordStatusEnum.FINISHED.getCode()))
                            .must(queryBuilder);
                    break;
                // 已完成
                case 1:
                    // 已完成口径：活动记录状态为 已完成
                    query.must(QueryBuilders.termQuery(toCamelCase(TIrisUserActivityRecordExample.ACTIVITY_STATUS), ActivityRecordStatusEnum.FINISHED.getCode()));
                    break;
                // 未开始
                case 4:
                    // 进行中定义：活动未结束(由于订单会出现延迟，任务结束时间+1天) 且 分享数 = 0 并且 佣金佣金 = 0 并且 预估佣金 = 0
                    query.must(QueryBuilders.rangeQuery(toCamelCase(
                                    TIrisUserActivityRecordExample.CURRENT_ACTIVITY_END_TIME)).gte(TimeUtils.getAfterDaysDate(1).getTime()))
                            .must(QueryBuilders.termQuery(toCamelCase(TIrisUserActivityRecordExample.COMMISSION), 0))
                            .must(QueryBuilders.termQuery(toCamelCase(TIrisUserActivityRecordExample.ESTIMATE_COMMISSION), 0))
                            .must(QueryBuilders.termQuery(toCamelCase(TIrisUserActivityRecordExample.SHARE_COUNT), 0));
                    break;
                default:
                    break;
            }
        }
    }

}
